package com.xiangban.xiangbanmall.aop;


import com.xiangban.xiangbanmall.exception.BizException;
import com.xiangban.xiangbanmall.fegin.UserServiceFeign;
import com.xiangban.xiangbanmall.model.res.ApiResult;
import com.xiangban.xiangbanmall.model.res.ApiResultCode;
import com.xiangban.xiangbanmall.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import static com.xiangban.xiangbanmall.utils.RedisUtil.fex;
import static com.xiangban.xiangbanmall.utils.RedisUtil.fex_shop;


/**
 * @author : manke
 * @Description
 * @Date: 2020/10/31 11:02
 */
@Component
@Slf4j
public class AnnotationParse {
    public final static int flag_no_token = 10001; // 未携带token
    public final static int flag_failed = 10002; // token验证失败
    public final static int flag_no_permission = 10003; //无权限
    public final static int flag_no_voucher = 10004; //未携带有效凭证
    public final static int flag_fail_voucher = 10005; //凭证认证失败
    public final static int pass = 0;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserServiceFeign userServiceFeign;

    public Integer Permission(Method method) throws Exception {

        if (method.isAnnotationPresent(Permission.class)) {
            //验证token
            String token = request.getHeader("token");
            log.info("自定义获取的token：" + token);
            if (token == null || "".equals(token)) {
                log.info("=====未携带token");
                return flag_no_token;
            }
            String permission = method.getAnnotation(Permission.class).type();
            if ("".equals(permission)) {
                ApiResult<Object> res = userServiceFeign.getUserInfo("");
                if (res.getDataMap() == null || "".equals(res.getDataMap())) {
                    log.info("=====token验证失败");
                    return flag_failed;
                }

            }

        }

        return pass;
    }
}
